﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;


namespace Soft
{

    /// <summary>
    /// 系统硬件信息
    /// </summary>
    public static class HardwareInformation
    {
         
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mi"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GlobalMemoryStatusEx(ref MEMORY_INFO mi);


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static ManagementObjectCollection GetDiskDrive()
        {
            return new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive").Get();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IEnumerable<ManagementObject> GetDiskDrive(DiskDriveType type)
        {
            string diskDriveType = type.ToString();
            foreach (ManagementBaseObject managementBaseObject in HardwareInformation.GetDiskDrive())
            {
                ManagementObject managementObject = (ManagementObject)managementBaseObject;
                if (managementObject.Properties["InterfaceType"].Value.ToString() == diskDriveType)
                {
                    yield return managementObject;
                }
            }
            //ManagementObjectCollection.ManagementObjectEnumerator managementObjectEnumerator = null;
            //yield break;
            //yield break;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static ManagementObjectCollection GetLogicalDiskToPartition()
        {
            return new ManagementObjectSearcher("select * from Win32_LogicalDiskToPartition").Get();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static MEMORY_INFO GetMemoryStatus()
        {
            MEMORY_INFO memory_INFO = default(MEMORY_INFO);
            memory_INFO.dwLength = (uint)Marshal.SizeOf(memory_INFO);
            HardwareInformation.GlobalMemoryStatusEx(ref memory_INFO);
            return memory_INFO;
        }

        
        /// <summary>
        /// 
        /// </summary>
        public static double CpuPercentProcessorTimeTotal
        {
            get
            {
                return HardwareInformation.mCpuPercentProcessorTimeTotalSearcher.Get().Cast<ManagementObject>().Average((ManagementObject e) => e["PercentProcessorTime"].ConvertTo(0.0, true));
            }
        }

        /// <summary>
        /// 获取cpu使用率百分比和内存使用率
        /// </summary>
        /// <returns></returns>
        public static double GetSystemLoad()
        {
            MEMORY_INFO memoryStatus = HardwareInformation.GetMemoryStatus();
            return HardwareInformation.CpuPercentProcessorTimeTotal + memoryStatus.dwMemoryLoad;
        }


        /// <summary>
        /// 
        /// </summary>
        private static ManagementObjectSearcher mCpuPercentProcessorTimeTotalSearcher = new ManagementObjectSearcher("select PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor where Name = '_Total'");


    }
}
